﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace VirtualTournaments.Frontend.MenuControls.GenericControls
{
    using Tournament = Services.Tournaments.Tournament;
    using TournamentsService = Services.Tournaments.TournamentsService;
    using Match = Services.Tournaments.Match;

    public partial class RoundTournamentPlayInfo : System.Web.UI.UserControl
    {
        public EventHandler GoBackButtonClicked;

        public string TournamentName
        {
            get 
            {
                return TournamentNameLabel.Text;
            }

            set 
            {
                TournamentNameLabel.Text = value;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            
            //Match[] journeyMatches = TournamentsService.GetTournamentNextJourneyMatches(TournamentsService.GetTournamentByName(TournamentName));
            //this.JourneyManager.SetMatches(journeyMatches);
        }

        public void InfoButton_OnClick(object sender, EventArgs e)
        {
            Button clickedButton = sender as Button;
            //TODO: what if null?
            if (clickedButton != null)
            {
                string teamName = clickedButton.Text;

                this.TeamInfoControler.TeamName = teamName;
                SetPlayersInTeam(teamName);
                this.RoundTournamentViewControler.ActiveViewIndex = 1;
            }

        }
        protected List<string> GetTeamPlayersList(string TeamName)
        {
            List<string> players = new List<string>();
            string player = string.Empty;
            for (int i = 0; i < 5; i++)
            {
                player = "Player " + i + ";" +
                            (20 + i) + ";" +
                            (180 + i) + ";" +
                            (80 + i) + ";" +
                            (4 + i) + ";" +
                            (3 + i) + ";" +
                            (2 + i) + ";" +
                            (5 + i) + ";" +
                            (2 + i);

                players.Add(player);
            }

            return players;
        }

        protected void SetPlayersInTeam(string TeamName)
        {
            List<string> players = GetTeamPlayersList(TeamName);
            TableRow row;
            TableCell cell;
            foreach (string player in players)
            {
                row = new TableRow();
                this.TeamInfoControler.TeamPlayersList.Items.Add(new ListItem(player));
                string[] playerAttributes = player.Split(';');
                foreach (string attribute in playerAttributes)
                {
                    cell = new TableCell();
                    cell.Text = attribute;
                    row.Cells.Add(cell);
                }
                this.TeamInfoControler.TeamPlayersTable.Rows.Add(row);
            }
        }

        protected void TeamInfoGoBackButton_OnClick(object sender, EventArgs e)
        {
            this.RoundTournamentViewControler.ActiveViewIndex = 0;
        }

        protected void TournamentInfoGoBackButton_OnClick(object sender, EventArgs e) 
        {
            GoBackButtonClicked(this, new VirtualTournamentsFrontendEvent { SenderMenu = "RoundTournamentPlayInfo" });
        }
        
        protected void GetNextJourneyButton_OnClick(object sender, EventArgs e)
        {
            //Tournament currentTournament = TournamentsService.GetTournamentByName(TournamentName);

            //Match[] journeyMatches = TournamentsService.GetTournamentNextJourneyMatches(currentTournament);
            //this.JourneyManager.SetMatches(journeyMatches);
        }

        protected void PlayMatchesButton_OnClick(object sender, EventArgs e)
        {
            //Tournament currentTournament = TournamentsService.GetTournamentByName(TournamentName);

            //TournamentsService.PlayNextJourney(currentTournament);

            //Match[] journeyMatches = TournamentsService.GetTournamentLastJourneyMatches(currentTournament);
            //this.JourneyManager.SetMatches(journeyMatches);
        }
    }
}